/**
 * Class:InsertCommentAction Class
 * Description:This ActionClass used for Insertion of a particular comment
 */
package com.actions;

import java.sql.Date;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.DAO.CommentDAO;
import com.DAO.NoticeDAO;
import com.beans.CommentForm;
import com.beans.NoticeForm;

public class AddCommentAction extends Action {
	private final static String SUCCESS = "success";

	/**
	 * This is the action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @throws java.lang.Exception
	 * @return
	 */
	
	@SuppressWarnings("finally")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("i am in Add comment action.............................");
		
		CommentDAO cmntdao = new CommentDAO();
		CommentForm commentForm = (CommentForm) form;
			
		    HttpSession session=request.getSession();
		    NoticeForm noticeForm=(NoticeForm)session.getAttribute("noticeform");
		    System.out.println("notice form is.........."+noticeForm);
		    
		    int notice_id=noticeForm.getNoticeId();
		    
		    
		   	String creatorid=session.getAttribute("userid").toString();
			//String creatorname=noticeForm.getCreatorName();
						
			commentForm.setCreatedById(creatorid);
			//commentForm.setCreatorname(creatorname);
			commentForm.setNoticeId(notice_id);
		
		    int insertedrows = 0;
	     	try {
			insertedrows = cmntdao.insert(commentForm);
			System.out.println("comment added..........."+insertedrows);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//int id=Integer.parseInt((request.getAttribute("notice_id")).toString());
		
		try{
		//int id=Integer.parseInt(request.getParameter("notice_id"));
		NoticeDAO noticeDAO=new NoticeDAO();
		ArrayList<CommentForm> listComments =new ArrayList<CommentForm>();
    	listComments=noticeDAO.getComments(notice_id);
    	
    	session.setAttribute("listComments", listComments);
		}
		catch(Exception e)
		{
			System.out.println("Notice is is not found");
		}
		
    	
    	
			//cmntdao.closeConnection();
			return mapping.findForward(SUCCESS);
	
	}
}
